package main

import (
	"fmt"
	"io"
	"net"
	"time"
	"zinx-tianjing-study/znet"
)

func main() {
	fmt.Println("client start")
	time.Sleep(1 * time.Second)

	coon, err := net.Dial("tcp", "127.0.0.1:8999")

	if err != nil {
		fmt.Println("连接失败:", err)
		return
	}

	for {

		dp := znet.GetDataPack()

		binaryData, err := dp.Pack(znet.NewMessage(1, []byte("Client1 测试中！！！！！")))

		if err != nil {
			fmt.Println("Pack err:", err)
			return
		}

		coon.Write(binaryData)

		//读取数据
		headData := make([]byte, dp.GetHeadLen())

		_, err = io.ReadFull(coon, headData)

		msg, err := dp.UnPack(headData)

		if msg.GetDataLen() > 0 {
			msg := msg.(*znet.Message)
			data := make([]byte, msg.GetDataLen())
			if _, err := io.ReadFull(coon, data); err != nil {
				fmt.Println("Read msg data err: ", err)
			}
			msg.SetData(data)
		}

		fmt.Println(string(msg.GetData()))

	}
}
